/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.menu.mainmenu;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import mtt.menu.events.ShowApplicationInfo;
import mtt.menu.events.ShowApplicationOptionEvent;
import mtt.menu.events.ShowCalendarEvent;
import mtt.menu.events.ShowMarksEvent;
import mtt.menu.mainmenu.events.ShowMainMenuViewEvent;
import hmvc.j2me.controller.AController;
import hmvc.j2me.events.CloseApplicationEvent;
import hmvc.j2me.events.IEvent;
import hmvc.j2me.events.ShowDisplayAbleEvent;

/**
 * 
 * 
 * @author jens.meiss
 */
public class MainMenuController extends AController implements CommandListener {

	private MainMenuView view;
	
	public MainMenuController() {
		
		setView(new MainMenuView());
		
		
	}

	public void init() {
		
		getView().setCommandExit(new Command("Beenden",
			Command.EXIT, 0));
		
		getView().setCommandSelect(new Command("Anzeigen",
			Command.ITEM, 0));
		
		getView().setListMenu(new List("MTT-Hauptmenü", List.IMPLICIT));
		
		addMenuEntries();
		
		register(ShowMainMenuViewEvent.class);
		
	}

	private MainMenuView getView() {
		return view;
	}

	public void receiveEvent(IEvent event) {
		
		if (event instanceof ShowMainMenuViewEvent) {			
			fireEventUp(new ShowDisplayAbleEvent(getView().getDisplayable()));			
		}
	}
	
	private void addMenuEntries() {
		/*
		 * add menu entries
		 */
		getView().getListMenu().insert(0, "Kalender", null);
		
		getView().getListMenu().insert(1, "Noten", null);
		
		getView().getListMenu().insert(2, "Optionen", null);
		
		getView().getListMenu().insert(3, "Info", null);

		getView().getListMenu().addCommand(getView().getCommandSelect());
		getView().getListMenu().addCommand(getView().getCommandExit());
		
		getView().getListMenu().setCommandListener(this);
		
	}
	
	public void commandAction(Command command, Displayable displayAble) {
		
		/*
		 * Button abfragen
		 */
		if (command == getView().getCommandExit()) {
			fireEventUp(new CloseApplicationEvent());
			return;
		}
		
		/*
		 * Listeneintrag ausgewählt
		 */
		int selectedIndex = getView().getListMenu().getSelectedIndex();

		switch (selectedIndex) {
		case 0:
			fireEventUp(new ShowCalendarEvent());
			break;
		case 1:
			fireEventUp(new ShowMarksEvent());
			break;
		case 2:
			fireEventUp(new ShowApplicationOptionEvent());
			break;
		case 3:
			fireEventUp(new ShowApplicationInfo());
			break;
		}
	}

	private void setView(MainMenuView mainMenuView) {
		this.view = mainMenuView;
	}

	public void terminate() {
	}

}
